package com.oa.pm.controller;

import com.joneying.common.web.response.Resp;
import com.oa.pm.dto.LaborContractAddDTO;
import com.oa.pm.dto.LaborContractParamDTO;
import com.oa.pm.entity.LaborContract;
import com.oa.pm.service.ILaborContractService;
import com.oa.pm.vo.LaborContractListVO;
import com.oa.pm.vo.LaborContractsListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.web.annotation.PostMapping;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

import javax.servlet.http.HttpServletResponse;

/**
 * @author syb
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/laborContract")
@Api(description = "劳动合同表模块")
public class LaborContractController extends BaseController {

    private final ILaborContractService laborContractService;

    @Autowired
    public LaborContractController(ILaborContractService laborContractService) {
        this.laborContractService = laborContractService;
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<LaborContract> findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(laborContractService.findLaborContractDetails(id));
    }

    /**
     * 新增
     *
     * @param laborContractAddDTO LaborContractAddDTO
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增劳动合同")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "劳动合同表对象") @RequestBody(required = false) LaborContractAddDTO laborContractAddDTO) {
        return laborContractService.add(laborContractAddDTO);
    }

    /**
     * 根据Id修改
     *
     * @param laborContractAddDTO LaborContractAddDTO
     * @return Resp
     */
    @PutMapping("")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "劳动合同表对象") @RequestBody(required = false) LaborContractAddDTO laborContractAddDTO) {
        return laborContractService.edit(laborContractAddDTO);
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        LaborContract laborContract = laborContractService.selectById(id);
        if (laborContract != null && laborContract.getIsDeleted() == 2) {
            laborContract.setIsDeleted(1);
            if (laborContractService.updateById(laborContract)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }

    @PostMapping("/list")
    @CommonController
    @Description("查询劳动合同列表")
    @ApiOperation(value = "查询劳动合同列表")
    public Resp<List<LaborContractsListVO>> findLaborContractList(@RequestBody Map condition) {
        return new Resp().success(laborContractService.findLaborContractList(condition));
    }

    @GetMapping("/export")
    @CommonController
    @Description("导出劳动合同列表")
    @ApiOperation(value = "导出劳动合同列表")
    public void export(Map condition){
        laborContractService.export(condition, response);
    }
}
